package com.example.smartcity.ui.frg

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity.R
import com.example.smartcity.adapter.NewsListAdapter
import com.example.smartcity.app.App
import com.example.smartcity.http.NewsListResp
import com.example.smartcity.ui.act.NewsDetailActivity
import kotlinx.android.synthetic.main.fragment_main_tab1_news_tab.*

/**
 * @Author: Tailyou(祝文飞)
 * @CreateDate: 2021/10/7 14:53
 * @Description: 首页新闻列表
 */
class MainNewsFrg : Fragment(R.layout.fragment_main_tab1_news_tab) {

    companion object {
        fun newInstance(id: Int): MainNewsFrg {
            var newsTabFrg = MainNewsFrg()
            var args = Bundle()
            args.putInt("id", id)
            newsTabFrg.arguments = args
            return newsTabFrg
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var id = arguments?.get("id")
        App.fetchData<NewsListResp>(
            "/prod-api/press/press/list?type=$id",
            NewsListResp::class.java
        ) {
            val linearLayoutManager = LinearLayoutManager(context)
            linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
            rvNews.layoutManager = linearLayoutManager
            rvNews.adapter = NewsListAdapter(it.rows,
                object : NewsListAdapter.OnItemClickListener {
                    override fun onClick(position: Int) {
                        val intent = Intent(context, NewsDetailActivity::class.java)
                        intent.putExtra("news_id", it.rows[position].id)
                        startActivity(intent)
                    }
                })
        }
    }

}